<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Fenghuangshuka extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        if ($bean->getCateId() == 26){
            $rechargeNum = ($bean->getRechargeNum() * $bean->getStartCharging()) . 'M';
        } elseif ($bean->getCateId() == 25){
            $rechargeNum = $bean->getRechargeNum() * $bean->getStartCharging();
        } else {
            $rechargeNum = '';
        }
        $please = [
            'userid'        => $bean->getSupplierNum(), //商户号
            'productid'     => '',//话费和流量为空
            'price'         => $rechargeNum, //面值 = 数量
            'num'           => '1',//数量 一般为一
            'mobile'        => $bean->getAccount(),//充值账号
            'spordertime'   => date('Y-m-d H:i:s',time()),
            'sporderid'     => $bean->getOrderNo(), //订单号
        ];

        $sign = urldecode(http_build_query($please).'&key=' . $bean->getSecretKey());
        $please['sign'] = md5($sign);
        $please['back_url'] =$bean->getNotifyUrl(); //异步回调地址
        $please['flowtype'] = '';
        return $please;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('resultno', '');
        $order_sn = $request->input('sporderid', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['1', '9']))->setRechargeSuccess($status == '1');
    }
}
